package cmo.neutech.day12.d1;

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class email {
    private String address;
    private String name;
    public email(String address,String name) {
        this.address = address;
        this.name = name;
    }

    public boolean isValid() {
        String exAddress = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$";
        Pattern pattern = Pattern.compile(exAddress);
        Matcher matcher = pattern.matcher(address);
        return matcher.matches();
    }

    public void getUsername() {
        if (!isValid()) {
            System.out.println("输入格式不合法");
        }

        int atIndex = address.indexOf('@');
        int dtIndex = address.indexOf('.');
        if(atIndex == -1 || dtIndex == -1){
            throw new IllegalStateException("邮箱地址不包含@或.");
        }
        int doubleAtIndex = address.indexOf('@',atIndex+1);
        if(doubleAtIndex != -1){
            throw new IllegalStateException("邮箱地址中包含多个@");
        }
        if(dtIndex < atIndex){
            throw new IllegalStateException(".出现在@前面");
        }
    }
}

